package com.engine4cloud.tdf.supervision.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.engine4cloud.tdf.common.core.exception.ValidateCodeException;
import com.engine4cloud.tdf.common.data.web.query.Pages;
import com.engine4cloud.tdf.common.data.web.query.Queries;
import com.engine4cloud.tdf.common.data.web.response.PageInfo;
import com.engine4cloud.tdf.common.data.web.response.Params;
import com.engine4cloud.tdf.supervision.convert.JzdxInterdictConvert;
import com.engine4cloud.tdf.supervision.dto.create.JzdxInterdictCreateDTO;
import com.engine4cloud.tdf.supervision.dto.query.JzdxInterdictQueryDTO;
import com.engine4cloud.tdf.supervision.dto.result.JzdxInterdictResultDTO;
import com.engine4cloud.tdf.supervision.dto.result.JzdxResultDTO;
import com.engine4cloud.tdf.supervision.dto.update.JzdxInterdictUpdateDTO;
import com.engine4cloud.tdf.supervision.entity.JzdxInterdictEntity;
import com.engine4cloud.tdf.supervision.mapper.JzdxInterdictMapper;
import com.engine4cloud.tdf.supervision.service.JzdxInterdictService;
import com.engine4cloud.tdf.supervision.service.JzdxService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * 矫正对象-禁止令
 *
 * @author wenjie
 * @date 2024-04-19 10:29:41
 */
@Service
public class JzdxInterdictServiceImpl extends ServiceImpl<JzdxInterdictMapper, JzdxInterdictEntity> implements JzdxInterdictService {
    @Resource
    private JzdxService jzdxService;

    @Override
    public PageInfo<JzdxInterdictResultDTO> pageList(Params pageable, JzdxInterdictQueryDTO dto) {
        IPage<JzdxInterdictResultDTO> page = this.baseMapper.pageList(Pages.page(pageable), dto);
        return Pages.convert(page);
    }

    @Override
    public JzdxInterdictResultDTO getById(Long id) {
        return JzdxInterdictConvert.INSTANCE.convert(super.getById(id));
    }

    @Override
    public Boolean create(JzdxInterdictCreateDTO dto) {
        JzdxResultDTO dx = Optional.ofNullable(jzdxService.getByCode(dto.getRybh()))
                .orElseThrow(() -> new ValidateCodeException("矫正对象数据不存在"));

        return super.save(JzdxInterdictConvert.INSTANCE.create(dto, dx.getTenantId()));
    }

    @Override
    public Boolean update(JzdxInterdictUpdateDTO dto) {
        Optional.ofNullable(super.getById(dto.getId()))
                .orElseThrow(() -> new ValidateCodeException("数据不存在"));

        return super.updateById(JzdxInterdictConvert.INSTANCE.update(dto));
    }

    @Override
    public JzdxInterdictResultDTO findLatest(String code) {
        return JzdxInterdictConvert.INSTANCE.convert(super.getOne(wrapperLatest(new JzdxInterdictQueryDTO(code))));
    }

    private LambdaQueryWrapper<JzdxInterdictEntity> wrapperLatest(JzdxInterdictQueryDTO query) {
        LambdaQueryWrapper<JzdxInterdictEntity> wrapper = Queries.lambda();
        Queries.accept(query.getRybh(), v -> wrapper.eq(JzdxInterdictEntity::getRybh, v));
        wrapper.orderByDesc(JzdxInterdictEntity::getCreateTime);
        wrapper.last("LIMIT 1");
        return wrapper;
    }
}